package com.itheima.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.itheima.common.R;
import com.itheima.domain.Admin;
import com.itheima.domain.AdminRoleRelation;
import com.itheima.domain.Menu;
import com.itheima.domain.Role;
import com.itheima.exception.BusinessException;
import com.itheima.incerceptor.AdminHolder;
import com.itheima.service.IAdminRoleRelationService;
import com.itheima.service.IAdminService;
import com.itheima.service.IMenuService;
import com.itheima.service.IRoleService;
import com.itheima.util.JwtUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class UmsAdminManager {
    @DubboReference
    IAdminService adminService;
    @DubboReference
    IAdminRoleRelationService adminRoleRelationService;
    @DubboReference
    IRoleService roleService;
    @DubboReference
    IMenuService menuService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    public R<Map<String,String>> login(Map<String,String> map) {
        //获取s用户名密码
        String username = map.get("username");
        String password = map.get("password");
        //查询管理员是否存在
        Admin one = adminService.findByUsername(username);

        //若不存在
        if(one==null){
            throw new BusinessException("用户名输入错误");
        }


        //判断密码是否正确
        if(!StrUtil.equals(one.getPassword(),SecureUtil.md5(password))){
            System.out.println(SecureUtil.md5(password));
            throw new BusinessException("密码输入错误");
        }

        one.setPassword(null);
        //生成token
        String token = JwtUtil.createToken(BeanUtil.beanToMap(one));

        //将token作为key,用户的json数组作为value写到redis中,时效1小时
        stringRedisTemplate.opsForValue().set(token, JSON.toJSONString(one), Duration.ofHours(1));
        Map<String,String> resultMap=new HashMap<>();
        resultMap.put("tokenHead","Bearer ");
        resultMap.put("token",token);

        return R.success(resultMap);
    }

    public R<Map<String, Object>> findAdminInfo(String name) {

        //获得登录者对象
        Admin admin = AdminHolder.get();
        if(admin==null) return R.error("错误");
        //获取id和icon
        Long id = admin.getId();
        String icon = admin.getIcon();

        //根据id查询用户角色对应关系表
        List<AdminRoleRelation> adminRoleRelations=adminRoleRelationService.findByAdminId(id);

        //获取用户对应的角色id，并通过角色id查询角色名称,并将角色名称存入集合
        List<String> roleNames=new ArrayList<>();
        adminRoleRelations.forEach(roleRelation -> {
            Long roleId = roleRelation.getRoleId();
            Role role=roleService.findByRoleId(roleId);
            roleNames.add(role.getName());
        });

        //查询全部菜单数据，并存入集合
        List<Menu> menus=menuService.findAll();

        //将数据封装进map集合中
        Map<String,Object> resultMap=new HashMap<>();
        resultMap.put("roles",roleNames);
        resultMap.put("icon",icon);
        resultMap.put("menus",menus);
        resultMap.put("username",admin.getUsername());
        return R.success(resultMap);
    }
}
